package com.visikard.ui.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.visikard.ui.R;

public class HistoryOptionFragment extends DialogFragment {

	public static final String KEY_SELECTED_OPTION = "key_selected_option";

	public interface IHistoryOption {
		public void onHistoryOptionDone(boolean[] options);
	}

	public static final int ALL_TRANSACTION = 0;
	public static final int COUPON_ONLY = 1;
	public static final int GIFTKARD_ONLY = 2;
	public static final int PUNCHKARD_ONLY = 3;
	public static final int FREESTUFF_ONLY = 4;
	public static final int PRODUCT_ONLY = 5;
	public static final int RTUI_COUPON_ONLY = 6;

	View optAllTrans, optCoupon, optGiftKard, optPunchKard, optFreeStuff, optProduct, optRtuiCoupon;
	ImageView imgAllTrans, imgCoupon, imgGiftKard, imgPunchKard, imgFreeStuff, imgProduct, imgRtuiCoupon;
	View txtCancel, txtDone;

	boolean[] mOptions = new boolean[] { false, false, false, false, false, false, false };

	IHistoryOption mListener;

	public void setListener(IHistoryOption listener) {
		mListener = listener;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setStyle(STYLE_NO_TITLE, 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.history_trans_selection, container, false);
		getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));

		optAllTrans = view.findViewById(R.id.opt_all);
		optCoupon = view.findViewById(R.id.opt_coupon);
		optGiftKard = view.findViewById(R.id.opt_giftkard);
		optPunchKard = view.findViewById(R.id.opt_punchkard);
		optFreeStuff = view.findViewById(R.id.opt_free_stuff);
		optProduct = view.findViewById(R.id.opt_product);
		optRtuiCoupon = view.findViewById(R.id.opt_rtui_coupon);

		imgAllTrans = (ImageView) view.findViewById(R.id.img_opt_all_trans);
		imgCoupon = (ImageView) view.findViewById(R.id.img_opt_coupon);
		imgGiftKard = (ImageView) view.findViewById(R.id.img_opt_giftkard);
		imgPunchKard = (ImageView) view.findViewById(R.id.img_opt_punchkard);
		imgFreeStuff = (ImageView) view.findViewById(R.id.img_opt_free_stuff);
		imgProduct = (ImageView) view.findViewById(R.id.img_opt_product);
		imgRtuiCoupon = (ImageView) view.findViewById(R.id.img_opt_rtui_coupon);

		txtCancel = view.findViewById(R.id.txt_cancel);
		txtDone = view.findViewById(R.id.txt_done);

		optAllTrans.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[ALL_TRANSACTION]) {
				// imgAllTrans.setImageResource(R.drawable.check_box_c);
				// imgCoupon.setImageResource(R.drawable.check_box_c);
				// imgGiftKard.setImageResource(R.drawable.check_box_c);
				// imgPunchKard.setImageResource(R.drawable.check_box_c);
				// imgFreeStuff.setImageResource(R.drawable.check_box_c);
				// imgProduct.setImageResource(R.drawable.check_box_c);
				// imgRtuiCoupon.setImageResource(R.drawable.check_box_c);
				// mOptions[COUPON_ONLY] = false;
				// mOptions[GIFTKARD_ONLY] = false;
				// mOptions[PUNCHKARD_ONLY] = false;
				// mOptions[FREESTUFF_ONLY] = false;
				// mOptions[PRODUCT_ONLY] = false;
				// mOptions[RTUI_COUPON_ONLY] = false;
				// }
				// else {
				// imgAllTrans.setImageResource(R.drawable.check_icon_c);
				// imgCoupon.setImageResource(R.drawable.check_icon_c);
				// imgGiftKard.setImageResource(R.drawable.check_icon_c);
				// imgPunchKard.setImageResource(R.drawable.check_icon_c);
				// imgFreeStuff.setImageResource(R.drawable.check_icon_c);
				// imgProduct.setImageResource(R.drawable.check_icon_c);
				// imgRtuiCoupon.setImageResource(R.drawable.check_icon_c);
				// mOptions[COUPON_ONLY] = true;
				// mOptions[GIFTKARD_ONLY] = true;
				// mOptions[PUNCHKARD_ONLY] = true;
				// mOptions[FREESTUFF_ONLY] = true;
				// mOptions[PRODUCT_ONLY] = true;
				// mOptions[RTUI_COUPON_ONLY] = true;
				// }
				// mOptions[ALL_TRANSACTION] = !mOptions[ALL_TRANSACTION];
				clearOptions();
				setSelectedOption(ALL_TRANSACTION);
			}
		});

		optCoupon.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[COUPON_ONLY]) {
				// imgCoupon.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgCoupon.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[COUPON_ONLY] = !mOptions[COUPON_ONLY];
				clearOptions();
				setSelectedOption(COUPON_ONLY);
			}
		});

		optGiftKard.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[GIFTKARD_ONLY]) {
				// imgGiftKard.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgGiftKard.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[GIFTKARD_ONLY] = !mOptions[GIFTKARD_ONLY];
				clearOptions();
				setSelectedOption(GIFTKARD_ONLY);
			}
		});

		optPunchKard.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[PUNCHKARD_ONLY]) {
				// imgPunchKard.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgPunchKard.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[PUNCHKARD_ONLY] = !mOptions[PUNCHKARD_ONLY];
				clearOptions();
				setSelectedOption(PUNCHKARD_ONLY);
			}
		});

		optFreeStuff.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[FREESTUFF_ONLY]) {
				// imgFreeStuff.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgFreeStuff.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[FREESTUFF_ONLY] = !mOptions[FREESTUFF_ONLY];
				clearOptions();
				setSelectedOption(FREESTUFF_ONLY);
			}
		});

		optProduct.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[PRODUCT_ONLY]) {
				// imgProduct.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgProduct.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[PRODUCT_ONLY] = !mOptions[PRODUCT_ONLY];
				clearOptions();
				setSelectedOption(PRODUCT_ONLY);
			}
		});

		optRtuiCoupon.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// if (mOptions[RTUI_COUPON_ONLY]) {
				// imgRtuiCoupon.setImageResource(R.drawable.check_box_c);
				// }
				// else {
				// imgRtuiCoupon.setImageResource(R.drawable.check_icon_c);
				// }
				// mOptions[RTUI_COUPON_ONLY] = !mOptions[RTUI_COUPON_ONLY];
				clearOptions();
				setSelectedOption(RTUI_COUPON_ONLY);
			}
		});

		txtCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		txtDone.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onHistoryOptionDone(mOptions);
				}
				dismiss();
			}
		});

		Bundle args = getArguments();
		if (args != null) {
			int selectedOption = args.getInt(KEY_SELECTED_OPTION, ALL_TRANSACTION);
			clearOptions();
			setSelectedOption(selectedOption);
		}

		return view;
	}

	private void clearOptions() {
		imgAllTrans.setImageResource(R.drawable.check_box_c);
		imgCoupon.setImageResource(R.drawable.check_box_c);
		imgGiftKard.setImageResource(R.drawable.check_box_c);
		imgPunchKard.setImageResource(R.drawable.check_box_c);
		imgFreeStuff.setImageResource(R.drawable.check_box_c);
		imgProduct.setImageResource(R.drawable.check_box_c);
		imgRtuiCoupon.setImageResource(R.drawable.check_box_c);
		mOptions[ALL_TRANSACTION] = false;
		mOptions[COUPON_ONLY] = false;
		mOptions[GIFTKARD_ONLY] = false;
		mOptions[PUNCHKARD_ONLY] = false;
		mOptions[FREESTUFF_ONLY] = false;
		mOptions[PRODUCT_ONLY] = false;
		mOptions[RTUI_COUPON_ONLY] = false;
	}

	private void setSelectedOption(int option) {
		switch (option) {
			case ALL_TRANSACTION:
				mOptions[ALL_TRANSACTION] = true;
				imgAllTrans.setImageResource(R.drawable.check_icon_c);
				imgCoupon.setImageResource(R.drawable.check_icon_c);
				imgGiftKard.setImageResource(R.drawable.check_icon_c);
				imgPunchKard.setImageResource(R.drawable.check_icon_c);
				imgFreeStuff.setImageResource(R.drawable.check_icon_c);
				imgProduct.setImageResource(R.drawable.check_icon_c);
				imgRtuiCoupon.setImageResource(R.drawable.check_icon_c);
				break;
			case COUPON_ONLY:
				mOptions[COUPON_ONLY] = true;
				imgCoupon.setImageResource(R.drawable.check_icon_c);
				break;
			case GIFTKARD_ONLY:
				mOptions[GIFTKARD_ONLY] = true;
				imgGiftKard.setImageResource(R.drawable.check_icon_c);
				break;
			case PUNCHKARD_ONLY:
				mOptions[PUNCHKARD_ONLY] = true;
				imgPunchKard.setImageResource(R.drawable.check_icon_c);
				break;
			case FREESTUFF_ONLY:
				mOptions[FREESTUFF_ONLY] = true;
				imgFreeStuff.setImageResource(R.drawable.check_icon_c);
				break;
			case PRODUCT_ONLY:
				mOptions[PRODUCT_ONLY] = true;
				imgProduct.setImageResource(R.drawable.check_icon_c);
				break;
			case RTUI_COUPON_ONLY:
				mOptions[RTUI_COUPON_ONLY] = true;
				imgRtuiCoupon.setImageResource(R.drawable.check_icon_c);
		}
	}

}
